package com.momei.security.handler;

import com.alibaba.fastjson.JSON;
import com.momei.common.entity.JsonResult;
import com.momei.common.enums.ResultCode;
import com.momei.common.utils.ResultTool;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *@description: 登录失败操作
 *@author: momo
 *@time: 2020/9/11
 */
@Component
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request,
                                        HttpServletResponse response,
                                        AuthenticationException e) throws IOException, ServletException {
         JsonResult result = ResultTool.fail(ResultCode.COMMON_FAIL);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(result));
    }
}